package com.hyt.it.ogt.api.ykcj;


import java.util.Collections;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.ykcj.common.core.domain.AjaxResult;
import com.hyt.it.ogt.ykcj.common.core.page.TableDataInfo;
import com.hyt.it.ogt.ykcj.common.enums.CommonEnums;
import com.hyt.it.ogt.ykcj.domain.exam.ExamInfo;
import com.hyt.it.ogt.ykcj.domain.vo.ExamInfoExternalVo;
import com.hyt.it.ogt.ykcj.domain.vo.QueryExamInfoVo;
import com.hyt.it.ogt.ykcj.service.exam.IExamInfoExternalService;
import com.hyt.it.ogt.ykcj.service.exam.IExamInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * description 对外提供考试信息查询接口Controller
 *
 * @author liy
 * @createTime 2022/05/16
 */
@Api(tags = "API.15.对外提供考试信息查询接口", value = "对外提供考试信息查询接口")
@ApiSort(value = 1005)
@Slf4j
@RestController
@RequestMapping("/exam/external/info")
public class ExamInfoApi extends BaseController {


	@Autowired
	private IExamInfoService examInfoService;


	@Autowired
	private IExamInfoExternalService examInfoExternalService;




	/**
	 * 获取考试列表信息
	 * 
	 * @throws Exception
	 */
	@ApiOperation(value = "获取考试列表信息",response =ExamInfoExternalVo.class)
	@ApiResponses(value = { @ApiResponse(code = 13000, message = "调用失败！"), })
	@PostMapping ("/getApiExamInfoExternalVos")
	public TableDataInfo getApiExamInfoExternalVos(@RequestBody QueryExamInfoVo queryExamInfoVo) {
		if(null == queryExamInfoVo.getPageNum()){
			queryExamInfoVo.setPageNum(1);
		}
		if(null == queryExamInfoVo.getPageSize()){
			queryExamInfoVo.setPageSize(10);
		}
		try {
			PageInfo<List<ExamInfoExternalVo>> pageInfo = examInfoExternalService.getExamInfoExternalVos( queryExamInfoVo);
			return getDataTable(pageInfo);
		}catch (Exception e){
			logger.error("getApiExamInfoExternalVos方法异常;{}",e);
			return getDataTable(new PageInfo(Collections.emptyList()));
		}
	}


	/**
	 * 获取考试详情信息
	 *
	 * @throws Exception
	 */
	@ApiOperation(value ="获取考试详情信息" ,response =ExamInfoExternalVo.class)
	@ApiResponses(value = { @ApiResponse(code = 13000, message = "调用失败！"), })
	@PostMapping("/detail")
	public AjaxResult detail(@RequestBody QueryExamInfoVo queryExamInfoVo) {
		try {
			if(CollectionUtils.isEmpty(queryExamInfoVo.getExamIds())){
				return AjaxResult.error("参数为空");
			}
			List<ExamInfoExternalVo> examInfoExternalVos= examInfoExternalService.getExamInfoExternalDetail(queryExamInfoVo.getExamIds());
			return AjaxResult.success(examInfoExternalVos);
		}catch (Exception e){
			logger.error("getExamInfoExternalDetail方法异常;{}",e);
			return AjaxResult.error();
		}

	}






	/**
	 * 绑定考试信息
	 *
	 * @throws Exception
	 */
	@ApiOperation("绑定考试信息")
	@ApiResponses(value = { @ApiResponse(code = 13000, message = "调用失败！"), })
	@GetMapping("/binding/{examId}")
	public AjaxResult binding(@PathVariable("examId")String examId) {
		ExamInfo examInfo = examInfoService.selectExamById(examId);
		examInfo.setBindingStatus(Integer.valueOf(CommonEnums.BINDING_STATUS_1.getCode()));
		int ret = examInfoService.updateExamInfo(examInfo);
		if(ret > 0){
			return AjaxResult.success();
		}
		return AjaxResult.error();
	}


	/**
	 * 解绑考试信息
	 *
	 * @throws Exception
	 */
	@ApiOperation("取消绑定")
	@ApiResponses(value = { @ApiResponse(code = 13000, message = "调用失败！"), })
	@GetMapping("/cancel/binding/{examId}")
	public AjaxResult cancelBinding(@PathVariable("examId")String examId) {

		ExamInfo examInfo = examInfoService.selectExamById(examId);
		examInfo.setBindingStatus(Integer.valueOf(CommonEnums.BINDING_STATUS_0.getCode()));
		int ret = examInfoService.updateExamInfo(examInfo);
		if(ret > 0){
			return AjaxResult.success();
		}
		return AjaxResult.error();
	}

}
